package com.yiyao.zxingdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends AppCompatActivity {

    EditText vHello;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vHello = (EditText) findViewById(R.id.hello);
    }

    public final void doScan(View v) {
        //Activity中这样使用
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.setOrientationLocked(false); //横屏两边可以切换
        integrator.initiateScan();

        //Fragment中这样使用
        //IntentIntegrator.forFragment(this).initiateScan();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                toast("没有扫描到结果");
            } else {
                String code = result.getContents();
                code = code + "\n";
                vHello.setText(code);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

}

/**
 Zxing超简单使用方法
 1，gradle插件需要3.0.1版本以上
 classpath 'com.android.tools.build:gradle:3.0.1'
 2，依赖，如下两个
 implementation 'com.android.support:design:25.3.1'
 implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
 3，调用扫描
 public final void doScan(View v) {
 //Activity中这样使用
 IntentIntegrator integrator = new IntentIntegrator(this);
 integrator.initiateScan();

 //Fragment中这样使用
 //IntentIntegrator.forFragment(this).initiateScan();
 }
 4，获取返回结果
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
 IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
 if (result != null) {
 if (result.getContents() == null) {
 toast("没有扫描到结果");
 } else {
 String code = result.getContents();
 code = code + "\n";
 vHello.setText(code);
 }
 } else {
 super.onActivityResult(requestCode, resultCode, data);
 }
 }
 5，可以了
 */
